﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ISS.IR.RRNMS.Library.BusinessLogic;
using ISS.IR.RRNMS.Library;

namespace ISS.IR.RRNMS.Web
{
    public partial class Administration : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                lstUsers.DataSource = IR_User.SelectAllUsers();
                lstUsers.DataBind();

                ddlUserRoles.DataSource = IR_User.SelectAllUserRoles();
                ddlUserRoles.DataBind();

                ddlUserRoles.Items.Insert(0, new ListItem()
                {
                    Value = "0",
                    Text = ""
                });

                ddlManagers.DataSource = IR_User.SelectAllUsers();
                ddlManagers.DataBind();

                ddlManagers.Items.Insert(0, new ListItem()
                {
                    Value = Guid.Empty.ToString(),
                    Text = ""
                });

                lstCategories.DataSource = Lookups.SelectAllEditableCategories();
                lstCategories.DataBind();

                lbSlots.DataSource = Slots.SelectListOfAllSlots();
              
                lbSlots.DataBind();
            }
        }

        protected void lstUsers_SelectedIndexChanged(object sender, EventArgs e)
        {
            Guid UserId = new Guid();

            if (lstUsers.SelectedIndex < 0 || String.IsNullOrEmpty(lstUsers.SelectedValue) || !Guid.TryParse(lstUsers.SelectedValue, out UserId)) return;

            IR_User user = new IR_User(UserId);

            txtFullName.Text = user.FullName;
            txtUserName.Text = user.Username;
            txtPassword.Text = user.Password;
            txtTelephone.Text = user.Telephone;
            txtMobile.Text = user.Mobile;
            txtEmail.Text = user.Email;

            chkHasAccountPerm.Checked = user.Permissions.HasAccountPerm;
            chkHasCalendarPerm.Checked = user.Permissions.HasCalendarPerm;
            chkHasSearchPerm.Checked = user.Permissions.HasSearchPerm;
            chkIsAdmin.Checked = user.Permissions.IsAdmin;


            ddlManagers.SelectedItem.Selected = false;
            if (user.Manager.GUID != Guid.Empty)
                ddlManagers.Items.FindByValue(user.Manager.GUID.ToString()).Selected = true;

            ddlUserRoles.SelectedItem.Selected = false;
            if (user.DefaultRole.Id != 0)
                ddlUserRoles.Items.FindByValue(user.DefaultRole.Id.ToString()).Selected = true;
        }

        protected void btnSaveUser_Click(object sender, EventArgs e)
        {
            IR_User user = new IR_User();

            if (lstUsers.SelectedIndex != -1 && lstUsers.SelectedValue != "")
                user.UserId = Guid.Parse(lstUsers.SelectedValue);

            user.FullName = txtFullName.Text;
            user.Username = txtUserName.Text;
            user.Password = txtPassword.Text;
            user.Telephone = txtTelephone.Text;
            user.Mobile = txtMobile.Text;
            user.Email = txtEmail.Text;

            user.Permissions = new UserPermissions()
            {
                HasAccountPerm = chkHasAccountPerm.Checked,
                HasCalendarPerm = chkHasCalendarPerm.Checked,
                HasSearchPerm = chkHasSearchPerm.Checked,
                IsAdmin = chkIsAdmin.Checked
            };

            if (ddlManagers.SelectedIndex > 0)
            {
                user.Manager = new Item()
                {
                    GUID = Guid.Parse(ddlManagers.SelectedValue),
                    Value = ddlManagers.SelectedItem.Text
                };
            }

            if (ddlUserRoles.SelectedIndex > 0)
            {
                user.DefaultRole = new Item()
                {
                    Id = Convert.ToInt32(ddlUserRoles.SelectedValue),
                    Value = ddlUserRoles.SelectedItem.Text
                };
            }

            user.Save();

            lstUsers.DataSource = IR_User.SelectAllUsers();
            lstUsers.DataBind();
        }



        protected void lstCategories_SelectedIndexChanged(object sender, EventArgs e)
        {
            int CategoryId = 0;

            if (String.IsNullOrEmpty(lstCategories.SelectedValue) || !Int32.TryParse(lstCategories.SelectedValue, out CategoryId)) return;

            lstCategoryItems.DataSource = Lookups.SelectCategoryItems(CategoryId);
            lstCategoryItems.DataBind();

            txtLookupValue.Text = "";
        }

        protected void lstCategoryItems_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtLookupValue.Text = "";
            txtLookupValue.Text = lstCategoryItems.SelectedItem.Text;
        }

        protected void btnSaveLookup_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtLookupValue.Text)) return;

            int CategoryId = (String.IsNullOrEmpty(lstCategories.SelectedValue)) || lstCategories.SelectedIndex < 0 ? 0 : Convert.ToInt32(lstCategories.SelectedValue);
            int ItemId = (String.IsNullOrEmpty(lstCategoryItems.SelectedValue)) || lstCategoryItems.SelectedIndex < 0 ? 0 : Convert.ToInt32(lstCategoryItems.SelectedValue);
            string LookupValue = txtLookupValue.Text;

            Lookups.SaveLookup(CategoryId, ItemId, LookupValue);

            if (String.IsNullOrEmpty(lstCategories.SelectedValue) || !Int32.TryParse(lstCategories.SelectedValue, out CategoryId)) return;

            lstCategoryItems.DataSource = Lookups.SelectCategoryItems(CategoryId);
            lstCategoryItems.DataBind();

            txtLookupValue.Text = "";
        }

        protected void btnNewLookup_Click(object sender, EventArgs e)
        {
            lstCategoryItems.SelectedItem.Selected = false;

            txtLookupValue.Text = "";
        }

        protected void btnDeleteLookup_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtLookupValue.Text)) return;

            int CategoryId = (String.IsNullOrEmpty(lstCategories.SelectedValue)) || lstCategories.SelectedIndex < 0 ? 0 : Convert.ToInt32(lstCategories.SelectedValue);
            int ItemId = (String.IsNullOrEmpty(lstCategoryItems.SelectedValue)) || lstCategoryItems.SelectedIndex < 0 ? 0 : Convert.ToInt32(lstCategoryItems.SelectedValue);


            Lookups.DeleteLookup(ItemId);

            if (String.IsNullOrEmpty(lstCategories.SelectedValue) || !Int32.TryParse(lstCategories.SelectedValue, out CategoryId)) return;

            lstCategoryItems.DataSource = Lookups.SelectCategoryItems(CategoryId);
            lstCategoryItems.DataBind();

            txtLookupValue.Text = "";
        }

        protected void btnDeleteUser_Click(object sender, EventArgs e)
        {
            IR_User user = new IR_User();
            if (lstUsers.SelectedIndex != -1 && lstUsers.SelectedValue != "")
                user.UserId = Guid.Parse(lstUsers.SelectedValue);
            user.Delete();

            lstUsers.DataSource = IR_User.SelectAllUsers();
            lstUsers.DataBind();
        }

        protected void btnNewUser_Click(object sender, EventArgs e)
        {
            lstUsers.SelectedItem.Selected = false;



            txtFullName.Text = "";
            txtUserName.Text = "";
            txtPassword.Text = "";
            txtTelephone.Text = "";
            txtMobile.Text = "";
            txtEmail.Text = "";

            chkHasAccountPerm.Checked = false;
            chkHasCalendarPerm.Checked = false;
            chkHasSearchPerm.Checked = false;
            chkIsAdmin.Checked = false;


            ddlManagers.SelectedItem.Selected = false;

            ddlUserRoles.SelectedItem.Selected = false;

        }

        protected void btnSaveSlot_Click(object sender, EventArgs e)
        {
            Slot s = new Slot();

            if (!String.IsNullOrEmpty(txtSlotFromHours.Text))
                s.FromHour = Convert.ToInt32(txtSlotFromHours.Text);

            if (!String.IsNullOrEmpty(txtSlotToHours.Text))
                s.ToHours = Convert.ToInt32(txtSlotToHours.Text);

            if (!String.IsNullOrEmpty(txtSlotFromMinutes.Text))
                s.FromMinutes = Convert.ToInt32(txtSlotFromMinutes.Text);

            if (!String.IsNullOrEmpty(txtSlotToMinutes.Text))
                s.ToMinutes = Convert.ToInt32(txtSlotToMinutes.Text);

            if (lbSlots.SelectedItem != null)
                s.SlotId = Guid.Parse(lbSlots.SelectedValue);

            Slots.SaveSlot(s);

            lbSlots.DataSource = Slots.SelectListOfAllSlots();

            lbSlots.DataBind();
        }

        protected void btnDeleteSlot_Click(object sender, EventArgs e)
        {
            if (lbSlots.SelectedIndex > -1 && lbSlots.SelectedValue != "")
                Slots.DeleteSlot(Guid.Parse(lbSlots.SelectedValue));
            lbSlots.DataSource = Slots.SelectListOfAllSlots();

            lbSlots.DataBind();

        }

        protected void btnNewSlot_Click(object sender, EventArgs e)
        {
            txtSlotFromHours.Text = "";
            txtSlotToHours.Text = "";
            txtSlotFromMinutes.Text = "";
            txtSlotToMinutes.Text = "";

            if (lbSlots.SelectedItem != null)
                lbSlots.SelectedItem.Selected = false;
        }

        protected void lbSlots_SelectedIndexChanged(object sender, EventArgs e)
        {
            Slot s = Slots.SelectSlot(Guid.Parse(lbSlots.SelectedValue));

            txtSlotFromHours.Text = s.FromHour.ToString();
            txtSlotToHours.Text = s.ToHours.ToString();
            txtSlotFromMinutes.Text = s.FromMinutes.ToString();
            txtSlotToMinutes.Text = s.ToMinutes.ToString();
        }
    }
}